package com.sky.controller.admin;

import com.sky.MyEnum.MessageEnum;
import com.sky.constant.MessageConstant;
import com.sky.result.Result;
import com.sky.utils.AliOssUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.swing.plaf.multi.MultiFileChooserUI;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * 通用接口
 */

@RestController
@RequestMapping("/admin/common")
@Api(tags = "通用接口")
@Slf4j
public class CommonController {

    @Autowired
    private AliOssUtil aliOssUtil;

    /**
     * @文件上传
     */
//    @PostMapping("/upload")
    @ApiOperation("文件上传")
    public Result<String> upload(MultipartFile file) {

        try {

            log.info("文件上传: {}", file);

            //原始文件名
            String originalFilename = file.getOriginalFilename();

            //截取原始文件名的后缀
            String extension = originalFilename.substring(originalFilename.lastIndexOf("."));

            //构造新文件名
            String objectName = UUID.randomUUID() + extension;

            //文件请求路径
            String filePath = aliOssUtil.upload(file.getBytes(), objectName);

            return Result.success(filePath);

        } catch (IOException e) {
           log.error("文件上传失败: {}", e);
        }

        return Result.error(MessageEnum.UPLOAD_FAILED.getExplain());


    }


    /**
     * 文件上传本地存储；
     * @param file
     * @return
     */
    @PostMapping("/upload")
    public Result<String>  uploadLocalhost( MultipartFile file) throws IOException {
      /*  log.info("文件上传本地存储参数；{}",file);
        String originalFilename = file.getOriginalFilename();

        String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
        String objectName=UUID.randomUUID().toString()+extension;
        //把临时文件存储到本地硬盘中；
        file.transferTo(new File("D:/localhost-image",objectName));
        commonService.saveImg(objectName);
        return null;*/

        if (file.isEmpty()) {
            return Result.error("图片上传失败");
        }
        //可以自己加一点校验 例如上传的是不是图片或者上传的文件是不是png格式等等 这里省略
        //获取原来的文件名和后缀
        String originalFilename = file.getOriginalFilename();
//        String ext = "." + FilenameUtils.getExtension(orgFileName); --需要导依赖
        String ext = "."+ originalFilename.split("\\.")[1];
        //生成一个新的文件名（以防有重复的名字存在导致被覆盖）
        String uuid = UUID.randomUUID().toString().replace("-", "");
        String newName = uuid + ext;
        //拼接图片上传的路径 url+图片名
        ApplicationHome applicationHome = new ApplicationHome(this.getClass());
        String pre = applicationHome.getDir().getParentFile().getParentFile().getAbsolutePath() + "\\src\\main\\resources\\static\\images\\";
        String path = pre + newName;
        try {
            file.transferTo(new File(path));
        } catch (IOException e) {
            e.printStackTrace();
        }

        log.info("图片存放路径 ==》{}", path);

        String requestPath = "static\\images\\" + newName;

        //获取图片请求
        log.info("获取图片请求路径 ==> {}", requestPath);

        return Result.success(path);
    }

//    @GetMapping()
//    public

}
